package cn.kw0501.tms.service.impl;

import cn.kw0501.tms.dao.SmsDao;
import cn.kw0501.tms.entity.Admin;
import cn.kw0501.tms.dao.AdminDao;
import cn.kw0501.tms.entity.Sms;
import cn.kw0501.tms.service.AdminService;
import cn.kw0501.tms.utils.CodeUtil;
import cn.kw0501.tms.utils.JsonResult;
import cn.kw0501.tms.utils.UuidUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统_管理员表 服务实现类
 * </p>
 *
 * @author Aedes
 * @since 2022-11-15
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {
    @Autowired
    AdminDao adminDao;

    @Autowired
    SmsDao smsDao;

    @Override
    public JsonResult loginPwd(Map<String, Object> parMap) {
        //根据手机号查询用户列表
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",parMap.get("phone").toString());
        List<Admin> users = adminDao.selectList(wrapper);

        //如果用户列表为空则用户不存在
        if(users==null||users.size()==0){
            return JsonResult.err(201,"用户不存在");
        }

        //如果用户数量大于1说明后台数据有问题
        if(users.size()!=1){
            return JsonResult.err(205,"请联系管理员");
        }

        //获取手机号
        String phone=parMap.get("phone").toString();
        //获取手机号的后4位为盐
        String salt=phone.substring(phone.length()-4);
        //调用检查密码的方法 将密码和盐和数据库的密码传入 如果为false则密码错误
        if(!CodeUtil.checkCode(parMap.get("password").toString(),salt,users.get(0).getPassword())){
            return JsonResult.err(1022,"账号或密码错误");
        }

        //给用户存入token
        String token = UuidUtil.getUUID();
        Admin adminToken = new Admin().setId(users.get(0).getId()).setToken(token);
        adminDao.updateById(adminToken);

        //将用户的密码改为空再传回前端
        users.get(0).setPassword(null);
        users.get(0).setToken(adminToken.getToken());
        return JsonResult.ok(users.get(0));
    }

    @Override
    public JsonResult loginCode(Map<String, Object> parMap) {
        //根据手机号查询用户列表
        QueryWrapper<Admin> wrapperA = new QueryWrapper<>();
        wrapperA.eq("phone",parMap.get("phone").toString());
        List<Admin> users = adminDao.selectList(wrapperA);

        //如果用户列表为空则用户不存在
        if(users==null||users.size()==0){
            return JsonResult.err(201,"用户不存在");
        }


        String phone = parMap.get("phone").toString();
        String code = parMap.get("code").toString();

        QueryWrapper<Sms> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone)
                .eq("code",code)
                .eq("state",0);

        List<Sms> list = smsDao.selectList(wrapper);
        if(list.size()==0){
            return JsonResult.err(1020,"验证失败,短信有误或已过期");
        }

        //修改短信状态
        Sms sms = new Sms()
                .setId(list.get(0).getId())
                .setState(1);
        smsDao.updateById(sms);


        //给用户存入token
        String token = UuidUtil.getUUID();
        Admin adminToken = new Admin().setId(users.get(0).getId()).setToken(token);
        adminDao.updateById(adminToken);

        //将用户的密码改为空再传回前端
        users.get(0).setPassword(null);
        users.get(0).setToken(adminToken.getToken());
        return JsonResult.ok(users.get(0));

    }

    @Override
    public Boolean isLogin(String token) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("token",token);
        List<Admin> users = adminDao.selectList(wrapper);
        if(users.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public JsonResult logout(String id) {
        adminDao.updateById(new Admin().setId(id).setToken(""));
        return JsonResult.ok(null);
    }
}
